package update.links.com.versionupdate;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.prim.update.UpdateManager;
import com.prim.update.model.Progress;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_update, btn_activity, cancel_time, cancel_version, btn_delete;

    private TextView tv_per;

    private String checkUrl = "http://rmrbtestfront.peopleapp.com/Api/600/MineApi/checkVersion?channal=baidu&city=%E4%B8%8A%E9%A5%B6%E5%B8%82&citycode=0793&device=6c883c26-5155-36e0-8b45-b8964eb872a0&device_model=oppo%20R11&device_os=Android%204.4.2&device_product=oppo&device_size=720*1244&device_type=1&district=%E9%84%B1%E9%98%B3%E5%8E%BF&fake_id=1322957&gtui_regist_id=f99bf51a8f041bded981a334406b72d8&interface_code=620&latitude=28.998157224494214&longitude=116.56832237363841&province=%E6%B1%9F%E8%A5%BF%E7%9C%81&province_code=1519895002000&version=6.2.0&securitykey=d67c64e092c5be197117eeb8656305f4";

    private static final String TAG = "MainActivity";

    private CheckBox cb_1, cb_2, cb_3, cb_4, cb_5, cb_6, cb_7, cb_8, cb_9, cb_10, cb_11, cb_12, cb_13, cb_15;

    private EditText time, name;

    private UpdatePrompter updatePrompter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_update = (Button) findViewById(R.id.btn_update);
        btn_activity = (Button) findViewById(R.id.btn_activity);
        cancel_time = (Button) findViewById(R.id.cancel_time);
        cancel_version = (Button) findViewById(R.id.cancel_version);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        tv_per = (TextView) findViewById(R.id.tv_per);
        cb_1 = (CheckBox) findViewById(R.id.cb_1);
        cb_2 = (CheckBox) findViewById(R.id.cb_2);
        cb_3 = (CheckBox) findViewById(R.id.cb_3);
        cb_4 = (CheckBox) findViewById(R.id.cb_4);
        cb_5 = (CheckBox) findViewById(R.id.cb_5);
        cb_6 = (CheckBox) findViewById(R.id.cb_6);
        cb_7 = (CheckBox) findViewById(R.id.cb_7);
        cb_8 = (CheckBox) findViewById(R.id.cb_8);
        cb_9 = (CheckBox) findViewById(R.id.cb_9);
        cb_10 = (CheckBox) findViewById(R.id.cb_10);
        cb_11 = (CheckBox) findViewById(R.id.cb_11);
        cb_12 = (CheckBox) findViewById(R.id.cb_12);
        cb_13 = (CheckBox) findViewById(R.id.cb_13);
        cb_15 = (CheckBox) findViewById(R.id.cb_15);
        time = (EditText) findViewById(R.id.time);
        name = (EditText) findViewById(R.id.name);
        btn_update.setOnClickListener(this);
        btn_activity.setOnClickListener(this);
        cancel_version.setOnClickListener(this);
        cancel_time.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        updatePrompter = new UpdatePrompter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_version:
                UpdateManager.getInstance().cancelIgnorable();
                break;
            case R.id.cancel_time:
                UpdateManager.getInstance().cancelPrompterTime();
                break;
            case R.id.btn_delete:
                UpdateManager.getInstance().deleteApkFile();
                break;
            case R.id.btn_update:
                checkUpdate();
                break;
            case R.id.btn_activity:
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void checkUpdate() {
        UpdateManager.getInstance()
                .setHttpClient(new xUtilsClient("1001"))
                .create(this)
                .setCheckUrl(checkUrl)
                .isWifiOnly(cb_2.isChecked())
                .setRequestType(UpdateManager.Builder.RequestType.GET)
                .setNotificationLargeIcon(R.mipmap.ic_launcher)
                .setNotificationSmallIcon(R.mipmap.ic_launcher_round)
                .setNotificationId(102)
                .setUpdatePrompter(cb_12.isChecked() ? updatePrompter : null)
                .setDownloadTitle(TextUtils.isEmpty(name.getText().toString()) ? "新版本更新" : name.getText().toString())
                .setStoragePath("/人民日报2")
                .isBackstage(cb_4.isChecked())
                .isAutoInstall(cb_6.isChecked())
                .isAutoDownload(cb_5.isChecked())
                .isDownloadAgain(cb_8.isChecked())
                .isNotificationShow(cb_1.isChecked())
                .isShowProgressDialog(cb_7.isChecked())
                .isNotWifiPrompter(cb_3.isChecked())
                .isEnforce(cb_9.isChecked())
                .isIgnorable(cb_10.isChecked())
                .prompterPriority(cb_13.isChecked())
                .isCycleUpdatePrompter(cb_11.isChecked())
                .isPatchUpdate(cb_15.isChecked())
                .setPrompterTime(TextUtils.isEmpty(time.getText().toString()) ? 0 : Integer.valueOf(time.getText().toString()))
                .check();
    }
}
